package bb.lanxing.activity.sport;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.mylhyl.circledialog.CircleDialog;

import java.util.List;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.sport.PanelSettingAdapter;
import bb.lanxing.adapter.sport.PanelSettingTabAdapter;
import bb.lanxing.model.sport.SportTypeGroup;
import bb.lanxing.utils.PanelItemTypeUtil;

public class PanelSettingActivity extends BaseActivity {
    public final SportTypeGroup[] CATEGORY = {new SportTypeGroup(R.string.sport_item_category_distance, new int[]{0}),
            new SportTypeGroup(R.string.sport_item_category_speed, new int[]{1, 3, 2, 4, 8, 9, 10}),
            new SportTypeGroup(R.string.sport_item_category_time, new int[]{5, 6, 7}),
            new SportTypeGroup(R.string.sport_item_category_altitude, new int[]{11, 42, 43}),
            new SportTypeGroup(R.string.sport_item_category_gradient, new int[]{12, 13, 44, 14, 16, 15, 17}),
            new SportTypeGroup(R.string.sport_item_category_generic, new int[]{38, 39}),
            new SportTypeGroup(R.string.sport_item_category_heart, new int[]{21, 22, 23, 24, 25}),
            new SportTypeGroup(R.string.sport_item_category_cadence, new int[]{18, 20, 19}),
            new SportTypeGroup(R.string.sport_item_category_power, new int[]{26, 27, 28, 32, 29, 30, 31, 33}),
            new SportTypeGroup(R.string.sport_item_category_direction, new int[]{40, 41}),
            new SportTypeGroup(R.string.sport_item_category_temperature, new int[]{35})};

    private TextView mSportItemBig;
    private TextView mSportItemBigName;

    private PanelSettingAdapter mPanelSettingAdapter = new PanelSettingAdapter();

    private LinearLayout tabPage;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    public PanelSettingTabAdapter mPanelSettingTabAdapter;

    private int sportType = 0;
    private List<Integer> mPanelItemTypeList;
    private int mItemType;
    private int mItemPosition = 0;
    private int mSelectedItemType = 0;
    /**
     * 下方默认选择哪一页
     */
    private int mSelectedTab = -1;
    private int mSelectedItem = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_panel_setting);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextView save = findViewById(R.id.save);
        save.setOnClickListener(v -> {
            PanelItemTypeUtil.savePanelSetting(App.getContext().getUserId(), sportType, mPanelItemTypeList);
            toast(getString(R.string.toast_save_successful));
            finish();
        });

        LinearLayout firstLayout = findViewById(R.id.firstLayout);
        firstLayout.setOnClickListener(v -> {
            mItemPosition = 0;
            firstLayout.setForeground(getDrawable(R.drawable.item_selected_fg));
            // 刷新 panel list
            mPanelSettingAdapter.updateSelectedItem(mItemPosition);
            mSelectedItemType = mItemType;
            setSelectedItem(mSelectedItemType);
        });
        mSportItemBig = findViewById(R.id.sport_item_big);
        mSportItemBigName = findViewById(R.id.sport_item_big_name);

        RecyclerView mRecyclerViewBig = findViewById(R.id.big_panel_list);
        mRecyclerViewBig.setLayoutManager(new GridLayoutManager(this, 2));
        mPanelSettingAdapter.setOnItemClickListener(position -> {
            mItemPosition = position + 1;
            firstLayout.setForeground(getDrawable(R.drawable.sport_item_edit_state));
            mPanelSettingAdapter.updateSelectedItem(mItemPosition);
            mSelectedItemType = mPanelItemTypeList.get(mItemPosition);
            setSelectedItem(mSelectedItemType);
        });
        mRecyclerViewBig.setAdapter(mPanelSettingAdapter);

        tabPage = findViewById(R.id.tabPage);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.pager);

        mPanelSettingTabAdapter = new PanelSettingTabAdapter(CATEGORY, getSupportFragmentManager());
        viewPager.setAdapter(mPanelSettingTabAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mPanelSettingTabAdapter.updateSelectedTab(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        sportType = getIntent().getIntExtra("sportType", 0);
        mPanelItemTypeList = PanelItemTypeUtil.loadCache(App.getContext().getUserId(), sportType);
        mPanelSettingAdapter.updateItem(mPanelItemTypeList);
        mItemType = mPanelItemTypeList.get(0);
        mSportItemBig.setText(PanelItemTypeUtil.resetDefault(mItemType));
        mSportItemBigName.setText(PanelItemTypeUtil.getItemName(mItemType));
    }

    public void setSportItem(int sportItemType) {
        mPanelItemTypeList.set(mItemPosition, sportItemType);
        if (mItemPosition == 0) {
            mItemType = sportItemType;
            mSportItemBig.setText(PanelItemTypeUtil.resetDefault(mItemType));
            mSportItemBigName.setText(PanelItemTypeUtil.getItemName(mItemType));
        } else {
            mPanelSettingAdapter.updateItem(mPanelItemTypeList);
        }
    }

    private void setSelectedItem(int sportItemType) {
        for (int i = 0; i < CATEGORY.length; i++) {
            for (int j = 0; j < CATEGORY[i].getTypes().length; j++) {
                if (sportItemType == CATEGORY[i].getTypes()[j]) {
                    mSelectedTab = i;
                    mSelectedItem = j;
                    break;
                }
            }
        }
        if (mSelectedTab < 0) {
            return;
        }
        tabLayout.getTabAt(mSelectedTab).select();
        mPanelSettingTabAdapter.mPanelSettingTypeFragments.get(mSelectedTab).getPanelSettingTypeAdapter()
                .updateSelectedItem(mSelectedTab, mSelectedItem);
        mPanelSettingTabAdapter.removeUnselectedItem();
        tabPage.setVisibility(View.VISIBLE);
        viewPager.setVisibility(View.VISIBLE);
    }

    @Override
    public void onBackPressed() {
        new CircleDialog.Builder().setTitle(getString(R.string.save)).setText(getString(R.string.save_current_settings))
                .setPositive(getString(R.string.save), v -> {
                    PanelItemTypeUtil.savePanelSetting(App.getContext().getUserId(), sportType, mPanelItemTypeList);
                    super.onBackPressed();
                    return true;
                }).setNegative(getString(R.string.cancel), v -> {
                    super.onBackPressed();
                    return true;
                }).show(getSupportFragmentManager());
    }
}